# Maintainer: George Rawlinson <grawlinson@archlinux.org>

pkgname=soju
pkgver=0.6.1
pkgrel=1
pkgdesc='A user-friendly IRC bouncer'
arch=('loong64' 'x86_64')
url='https://soju.im'
license=('AGPL3')
depends=('glibc')
makedepends=(
  'go'
  'scdoc'
  'sqlite'
)
optdepends=(
  'postgresql: PostgreSQL support'
  'sqlite: SQLite support'
)
backup=('etc/soju/config')
options=('!lto')
source=(
  "https://git.sr.ht/~emersion/soju/refs/download/v$pkgver/$pkgname-$pkgver.tar.gz"{,.sig}
  'systemd.service'
  'sysusers.conf'
  'tmpfiles.conf'
)
sha512sums=('12c939a050bf7276ee9e8fec05285533907a2787530ebe113a6be012cbfbcaac3c5cb5c42c3791618136cd45df6c9fb5739bcbec068650bf0d7ea2995443c0bf'
            'SKIP'
            'f1cbf40994dabe6c27288f1d3e5a78ed89a529a834ab178ddd64c093646d85ecc23ecabb5f9a20e99b805f46bb49fe321188b000fb1a3d59a64b77d8fde043c3'
            '7b4069043d204581b0eda376956f8996365631e83ca78e002d6ea0caec931605bb5e99cf4f42a75b6de958267b982ee8ed7f31046c6b97d8f2442430678f3c07'
            '378dd64a8c3fa4fd1b28c055bbeb7c46eb05ec79a925d5b138af5d373970e82bafba62ff29269f63e67c67f34fb961570d448a02fa08f7f6cf4e8d844a3c12a0')
b2sums=('3e61f13a2f711c9a1a280c158113fd75da53525c32cf8d04cf5459f25e55286e090fbf64c844e19bdf60780a9db37ee45976714500b12e92023330a426706c23'
        'SKIP'
        'fe0819958384f58bd04cb7022bd496538a9e70e532256bdbe92832b0e786c003dd2eac6199f2a343764ac50893dda1d85b0977a110aa67489fd7e3a20d4e4bdf'
        '14e951b98e80d0dddf08aa0c2b03c57a86bda5ea1fd62d332e56edaf42f478ff86e9367699ab83b1ce0b968068e31048573b703bca6f4900cea901ad518a94b7'
        '9d164e694980e40955b1e8cedf1875214299840a8fce09b20069d594e74265c4335c0693e4bdd7c073c062a1de66cca859657f7581a27baffbfab741f45d0acc')
validpgpkeys=('34FF9526CFEF0E97A340E2E40FDE7BE0E88F5E48')

prepare() {
  cd "$pkgname-$pkgver"

  # create directory for build output
  mkdir build

  # download dependencies
  export GOPATH="${srcdir}"
  go mod download
}

build() {
  cd "$pkgname-$pkgver"

  # set Go flags
  export CGO_CPPFLAGS="${CPPFLAGS}"
  export CGO_CFLAGS="${CFLAGS}"
  export CGO_CXXFLAGS="${CXXFLAGS}"
  export GOPATH="${srcdir}"

  go build -v \
    -buildmode=pie \
    -mod=readonly \
    -modcacherw \
    -ldflags "-compressdwarf=false \
    -linkmode external \
    -extldflags ${LDFLAGS}" \
    -tags 'libsqlite3' \
    -o build \
    ./cmd/... \
    ./contrib/...

  # rename binaries to prevent file conflicts
  mv build/{migrate-db,/soju-migrate-db}
  mv build/{znc-import,soju-znc-import}

  # man page
  scdoc < doc/soju.1.scd > build/soju.1
}

check() {
  cd "$pkgname-$pkgver"

  go test -v ./...
}

package() {
  # systemd integration
  install -vDm644 systemd.service "$pkgdir/usr/lib/systemd/system/$pkgname.service"
  install -vDm644 sysusers.conf "$pkgdir/usr/lib/sysusers.d/$pkgname.conf"
  install -vDm644 tmpfiles.conf "$pkgdir/usr/lib/tmpfiles.d/$pkgname.conf"

  cd "$pkgname-$pkgver"

  # binaries
  install -vDm755 -t "$pkgdir/usr/bin" build/soju{,ctl,-migrate-db,-znc-import}
  install -vDm755 contrib/casemap-logs.sh "$pkgdir/usr/bin/soju-casemap-logs"

  # man page
  install -vDm644 -t "$pkgdir/usr/share/man/man1" build/soju.1

  # documentation
  install -vDm644 -t "$pkgdir/usr/share/doc/$pkgname" README.md contrib/clients.md
  cp -vr doc "$pkgdir/usr/share/doc/$pkgname"
  rm -f "$pkgdir/usr/share/doc/$pkgname/doc/soju.1.scd"

  # configuration
  install -vDm644 config.in "$pkgdir/etc/soju/config"
}
